<template>
  <div class="edit">
    <van-nav-bar fixed title="Edit" left-arrow :border="false" @click-left="handleBack" />
    <UnLogin v-if="!hasLogin"></UnLogin>
    <AddForm :goods="detail" @success="handleSuccess" v-if="hasLogin && detail.id"></AddForm>
  </div>
</template>
<script>
import Api from "@/api";
import { isLogin } from "@/utils";
import UnLogin from "@/components/unlogin";
import AddForm from "@/components/add-form";

export default {
  name: "Edit",
  components: { UnLogin, AddForm },
  data() {
    return {
      hasLogin: false,
      id: "",
      detail: {},
    };
  },
  created() {
    this.id = this.$route.query.id;
    this.hasLogin = isLogin();
    this.hasLogin && this.loadDetail();
  },
  methods: {
    loadDetail() {
      Api.goodsDetail({ id: this.id }).then((resp) => {
        this.detail = resp;
      });
    },
    handleSuccess() {
      this.$toast({
        message: "Success!",
        duration: 1000,
        onClose: () => {
          this.$router.back();
        },
      });
    },
    handleBack() {
      this.$router.back();
    },
  },
};
</script>
<style lang="less" scoped>
.edit {
  padding-top: 50px;
}
</style>
